package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"time"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserProjectRefund
//@description: 创建UserProjectRefund记录
//@param: userProjectRefund model.UserProjectRefund
//@return: err error

func CreateUserProjectRefund(userProjectRefund model.UserProjectRefund) (err error) {
	err = global.GVA_DB.Create(&userProjectRefund).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserProjectRefund
//@description: 删除UserProjectRefund记录
//@param: userProjectRefund model.UserProjectRefund
//@return: err error

func DeleteUserProjectRefund(userProjectRefund model.UserProjectRefund) (err error) {
	err = global.GVA_DB.Delete(&userProjectRefund).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserProjectRefundByIds
//@description: 批量删除UserProjectRefund记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserProjectRefundByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserProjectRefund{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserProjectRefund
//@description: 更新UserProjectRefund记录
//@param: userProjectRefund *model.UserProjectRefund
//@return: err error

func UpdateUserProjectRefund(userProjectRefund model.UserProjectRefund) (err error) {
	err = global.GVA_DB.Save(&userProjectRefund).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserProjectRefund
//@description: 根据id获取UserProjectRefund记录
//@param: id uint
//@return: err error, userProjectRefund model.UserProjectRefund

func GetUserProjectRefund(id uint) (err error, userProjectRefund model.UserProjectRefund) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`UserProject`).First(&userProjectRefund).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserProjectRefundInfoList
//@description: 分页获取UserProjectRefund记录
//@param: info request.UserProjectRefundSearch
//@return: err error, list interface{}, total int64

func GetUserProjectRefundInfoList(info request.UserProjectRefundSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserProjectRefund{})
    var userProjectRefunds []model.UserProjectRefund
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.ProjectId != 0 {
        db = db.Where("`project_id` = ?",info.ProjectId)
    }
    if info.Status != 0 {
        db = db.Where("`status` = ?",info.Status)
    }
    if info.UserId != 0 {
        db = db.Where("`user_id` = ?",info.UserId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`UserProject`).Find(&userProjectRefunds).Error
	return err, userProjectRefunds, total
}

func GetNeal(info request.UserProjectRefundSearch)  (err error, list interface{}, total int64){
	var userProjectRefunds []model.UserProjectRefund
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	now := time.Now().Format(`2006-01-02`)+` 00:00:00`
	afterTime := time.Now().Add(time.Hour*24*7).Format(`2006-01-02`)+` 00:00:00`
	db := global.GVA_DB.Model(&model.UserProjectRefund{})
	db =db.Where(`expect_date >= ? and expect_date<=?`,now,afterTime).Group(`project_id`)
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`UserProject`).Order(`pay_date asc,expect_date asc`).Find(&userProjectRefunds).Error
	return err, userProjectRefunds, total
}